"use strict";

import http from "../../utils/http";

const groupService = {
  groupTree: function (resolve, reject) {
    http({
      url: "/user/groups",
      method: "get",
    })
      .then((res) => resolve(res))
      .catch((res) => reject(res));
  },
  addGroup: function ({ parentId, name }, resolve, reject) {
    http({
      url: "/user/group",
      data: { parentid: parentId, name },
      method: "post",
    })
      .then((res) => resolve(res))
      .catch((res) => reject(res));
  },
  editGroup: function ({ id, name }, resolve, reject) {
    http({
      url: "/user/group",
      data: { id, name },
      method: "put",
    })
      .then((res) => resolve(res))
      .catch((res) => reject(res));
  },
  deleteGroup: function (id, resolve, reject) {
    http({
      url: "/user/group",
      data: { id },
      method: "delete",
    })
      .then((res) => resolve(res))
      .catch((res) => reject(res));
  },
  users: function (resolve, reject) {
    http({
      url: "/user/users",
      method: "get",
    })
      .then((res) => resolve(res))
      .catch((res) => reject(res));
  },
  groupUsers: function (groupId, resolve, reject) {
    http({
      url: "/user/user_by_group",
      params: { groupId },
      method: "get",
    })
      .then((res) => resolve(res))
      .catch((res) => reject(res));
  },
  addGroupUsers: function ({ groupId, userIds }, resolve, reject) {
    http({
      url: "/user/group_user",
      data: { groupId, userIds },
      method: "post",
    })
      .then((res) => resolve(res))
      .catch((res) => reject(res));
  },
  deleteGroupUser: function ({ groupId, userId }, resolve, reject) {
    http({
      url: "/user/group_user",
      data: { groupId, userId },
      method: "delete",
    })
      .then((res) => resolve(res))
      .catch((res) => reject(res));
  },
  setUserBoss: function (id, resolve, reject) {
    http({
      url: "/user/boss",
      data: { id },
      method: "put",
    })
      .then((res) => resolve(res))
      .catch((res) => reject(res));
  },
};

export default groupService;
